﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Web;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;

namespace HZ.DrugTrade.Web.Hospitals.SalesReturn
{
    public partial class ReturnGoodsOderListUnSubmit : BasePage
    {
        public ReturnGoodsOderListUnSubmit()
        {
            this.ModuleNo = Module.退货单管理.ToString();
        }

        IBLL_Purchase I_Purchase = Factory.BLL_Purchase();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                Bind();
            }
        }


        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            Bind();
        }

        protected void Bind()
        {
            HZ.DrugTrade.Model.Query.ReturnMerchandise aReturnMerchandise = new HZ.DrugTrade.Model.Query.ReturnMerchandise();

            aReturnMerchandise.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            aReturnMerchandise.ReturnOrderStatus = 1;
            aReturnMerchandise.ReturnName = txtReturnlName.Text.Trim();
            aReturnMerchandise.ReturnTimeFrom = new HZ.Common.ConvertHelper(txtReturnTimeBegin.Text.Trim()).ToDateTime;
            aReturnMerchandise.ReturnTimeTo = new HZ.Common.ConvertHelper(txtReturnTimeEnd.Text.Trim()).ToDateTime;
            aReturnMerchandise.SortFields = " addtime DESC ";
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalrecord = 0;
            try
            {
                gvwProduceCatalog.AutoGenerateColumns = false;
                gvwProduceCatalog.DataSource = I_Purchase.GetReturnReturnMerchandise(aReturnMerchandise, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out totalrecord);
                gvwProduceCatalog.DataBind();
                this.AspNetPager1.RecordCount = totalrecord;
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }

        /// <summary>
        /// 多参连接
        /// </summary>
        /// <param name="ReturnSerialID"></param>
        /// <param name="ReturnName"></param>
        /// <returns></returns>
        public string getstring(object ReturnSerialID, object ReturnName, object ReturnOrderStatus)
        {

            return @"<a href='RetrunGoodsOderUpdate.aspx?TIDN=" + ReturnSerialID.ToString() + "&TINN=" + HttpUtility.UrlEncode(ReturnName.ToString()) + "&TST=" + ReturnOrderStatus + "'/>";

        }
        /// <summary>
        /// 根据退货单状态返回中文状态
        /// </summary>
        /// <param name="status"></param>
        /// <returns></returns>
        public string Getstatus(object status)
        {
            string strStatus = "未知";
            switch (status.ToString())
            {
                case "0":
                    strStatus = "空单";
                    break;
                case "1":
                    strStatus = "未提交";
                    break;
                case "2":
                    strStatus = "已提交";
                    break;
            }
            return strStatus;
        }
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            Bind();
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {

        }
        /// <summary>
        /// 提交退货单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //protected void btnAdd_Click(object sender, EventArgs e)
        //{
        //    Updatedate(1);
        //}
        /// <summary>
        /// 删除退货单
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDele_Click(object sender, EventArgs e)
        {
            Updatedate(2);
        }
        /// <summary>
        /// 公共调用操作退货单
        /// </summary>
        /// <param name="ntype"> 1 提交 2 删除  </param>
        private void Updatedate(int ntype)
        {
            try
            {
                List<HZ.DrugTrade.Model.ReturnMerchandise> DistributionBatchRecordList = new List<HZ.DrugTrade.Model.ReturnMerchandise>();
                if (new HZ.Common.ConvertHelper(Request.Form["chkReturn"]) != null)
                {
                    string[] strReturnSerialID = Request.Form["chkReturn"].ToString().Split(new char[] { ',' });//
                    for (int i = 0; i < strReturnSerialID.Length; i++)
                    {
                        if (gvwProduceCatalog.Rows[i].RowType == DataControlRowType.DataRow)
                        {

                            Model.ReturnMerchandise aReturnmer = new ReturnMerchandise();
                            //配送单ID
                            string strProcureCatalogID = strReturnSerialID[i].ToString();
                            aReturnmer.ReturnSerialID = strProcureCatalogID;
                            DistributionBatchRecordList.Add(aReturnmer);
                        }

                    }
                }
                if (DistributionBatchRecordList.Count > 0)
                {
                    IBLL_Purchase I_Purchase = Factory.BLL_Purchase();

                    int nSuccess = 0;
                    if (ntype == 1) //提交
                    {
                        nSuccess = I_Purchase.SubmitReturnMerchandise_Batch_Hospital(DistributionBatchRecordList);
                    }
                    else             //删除
                    {
                        nSuccess = I_Purchase.DeleteReturnMerchandise_Batch_Hospital(DistributionBatchRecordList);
                    }
                    if (nSuccess == 1)
                    {
                        HZ.Web.WebUtil.Alert("操作成功", this);

                        Bind();
                    }
                    else if (nSuccess == 2)
                    {
                        HZ.Web.WebUtil.Alert("退货单已提交！", this);
                        Bind();
                    }
                    else if (nSuccess == 0)
                    {
                        HZ.Web.WebUtil.Alert("操作失败", this);
                        Bind();
                    }
                    else if (nSuccess==2)
                    {
                        HZ.Web.WebUtil.Alert("有订单发票已开不能退货", this);
                    }
                }
                
            }
            catch (Exception ex)
            {
                PageError("数据库连接失败！", "");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void gvwProduceCatalog_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "submit")//提交退货单
            {
                HZ.DrugTrade.Model.Query.ReturnMerchandiseDetail aReturnMerchandise = new HZ.DrugTrade.Model.Query.ReturnMerchandiseDetail();
                aReturnMerchandise.HospitalID = CurrentAccount.GetUser().Org.OrgID;
                aReturnMerchandise.ReturnSerialID = e.CommandArgument.ToString();
                int totalrecord = 0;
                IList<Model.View.ReturnMerchandiseDetail> ReturnMerchandiseDetailList = I_Purchase.GetReturnReturnMerchandiseDetail(aReturnMerchandise, 1, 0, out totalrecord);
                if (totalrecord == 0)
                {
                    HZ.Web.WebUtil.Alert("退货单中没有任何产品，无法提交！", this);
                    return;
                }
                Model.ReturnMerchandise aReturnmer = new ReturnMerchandise();
                aReturnmer.ReturnSerialID = e.CommandArgument.ToString();
                List<HZ.DrugTrade.Model.ReturnMerchandise> DistributionBatchRecordList = new List<HZ.DrugTrade.Model.ReturnMerchandise>();
                DistributionBatchRecordList.Add(aReturnmer);
                int ss=I_Purchase.SubmitReturnMerchandise_Batch_Hospital(DistributionBatchRecordList);
                if (ss== 1)
                {
                    HZ.Web.WebUtil.Alert("操作成功", this);
                    Bind();
                }
                else if (ss == 2)
                {
                    HZ.Web.WebUtil.Alert("有订单发票已开不能退货", this);
                    Bind();
                }
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.ReturnMerchandise aReturnMerchandise = new HZ.DrugTrade.Model.Query.ReturnMerchandise();

            aReturnMerchandise.HospitalID = CurrentAccount.GetUser().Org.OrgID;
            aReturnMerchandise.ReturnOrderStatus = 1;
            aReturnMerchandise.ReturnName = txtReturnlName.Text.Trim();
            aReturnMerchandise.ReturnTimeFrom = new HZ.Common.ConvertHelper(txtReturnTimeBegin.Text.Trim()).ToDateTime;
            aReturnMerchandise.ReturnTimeTo = new HZ.Common.ConvertHelper(txtReturnTimeEnd.Text.Trim()).ToDateTime;
            aReturnMerchandise.SortFields = " addtime DESC ";
            string where = HZ.DrugTrade.DAL2.DAL_Helper.GetWhereCondition<HZ.DrugTrade.Model.Query.ReturnMerchandise>(aReturnMerchandise, true);
            string sql = string.Format(@"SELECT ReturnName 退货单名称,ReturnAmount 退货总额,ReturnTime 退货时间,case ReturnOrderStatus when 1 then '未提交' when 2 then '已提交' else '空单' end as  退货单状态,Remarks 备注 FROM View_ReturnMerchandise WHERE 1=1 {0}", where);
            HZ.DrugTrade.BLL.ExcelHelper.ExportByWeb(HZ.Data.DbHelperFactory.Create().ExecuteTable(sql), "", "退货单未提交.xls");
        }
    }
}
